package jp.co.radiation;

import jp.co.radiation.provider.RadiationManager;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TwoLineListItem;

public class DateListActivity extends FragmentActivity {
    public static final String EXTRA_SELECTED_DATE = "SelectedDate";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fm = getSupportFragmentManager();

        // Create the list fragment and add it as our sole content.
        if (fm.findFragmentById(android.R.id.content) == null) {
            DateListFragment list = new DateListFragment();
            fm.beginTransaction().add(android.R.id.content, list).commit();
        }
    }
    public static class DateListFragment extends ListFragment implements
            LoaderManager.LoaderCallbacks<Cursor> {
        // This is the Adapter being used to display the list's data.
        SimpleCursorAdapter mAdapter;

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            // Give some text to display if there is no data. In a real
            // application this would come from a resource.
            setEmptyText("No gps data");

            // // We have a menu item to show in action bar.
            // setHasOptionsMenu(true);

            // Create an empty adapter we will use to display the loaded data.
            mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
                    null, new String[] { RadiationManager.Radiation.DATE },
                    new int[] { android.R.id.text1 }, 0);
            setListAdapter(mAdapter);

            // Prepare the loader. Either re-connect with an existing one,
            // or start a new one.
            getLoaderManager().initLoader(0, null, this);
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // Insert desired behavior here.
            TwoLineListItem view = (TwoLineListItem) v;
            String returnDateValue = view.getText1().getText().toString();
            Log.i("FragmentComplexList", "Item clicked: " + id);
            FragmentActivity activity = getActivity();
            Intent intent = new Intent();
            intent.putExtra(EXTRA_SELECTED_DATE, returnDateValue);
            activity.setResult(RESULT_OK, intent);
            activity.finish();
            
        }

        // These are the Contacts rows that we will retrieve.
        static final String[] DATE_SUMMARY_PROJECTION = new String[] {
                RadiationManager.Radiation._ID, //
                RadiationManager.Radiation.DATE, //
                 };

        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            // This is called when a new Loader needs to be created. This
            // sample only has one Loader, so we don't care about the ID.
            // First, pick the base URI to use depending on whether we are
            // currently filtering.
            Uri baseUri = RadiationManager.Radiation.CONTENT_URI;
            String selection = "_id is not null) GROUP BY (" + RadiationManager.Radiation.DATE;

            return new CursorLoader(getActivity(), baseUri, DATE_SUMMARY_PROJECTION, selection,
                    null, RadiationManager.Radiation.DATE + " ASC");
        }

        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            // Swap the new cursor in. (The framework will take care of closing the
            // old cursor once we return.)
            mAdapter.swapCursor(data);
        }

        public void onLoaderReset(Loader<Cursor> loader) {
            // This is called when the last Cursor provided to onLoadFinished()
            // above is about to be closed. We need to make sure we are no
            // longer using it.
            mAdapter.swapCursor(null);
        }
    }
}